package com.feihong.txgw.tx_precenter;

import com.feihong.txgw.inter.contract.kaika.IKaikaContract;
import com.feihong.txgw.json.kaika.ICCIDList;
import com.feihong.txgw.tx_model.KaikaMainModel;
import com.feihong.txgw.utils.helper.RxHelper;
import com.feihong.txgw.utils.manage.RetrofitManager;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

/**
 * @Author wangziheng
 * @ProjectName txgw_app
 * @ClassName KaikaMainPrecenter
 * @package com.feihong.txgw.tx_precenter
 * @data 2018/5/4 11:43
 * @Describe
 * @Change
 */
public class KaikaMainPrecenter extends IKaikaContract.AbKaikaMainPrecenter {

    public static KaikaMainPrecenter create() {
        return new KaikaMainPrecenter();
    }

    @Override
    public IKaikaContract.IKaikaMainModel getModel() {
        return KaikaMainModel.create();
    }

    @Override
    public void onStart() {

    }

    @Override
    public void loadICCIDListOrValiData(final String lastNum, String orderSn) {
        RetrofitManager.getInstance().valiDataICCID(lastNum, orderSn)
                .compose(RxHelper.<ICCIDList>rxSchedulerHelper())
                .subscribe(new Observer<ICCIDList>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        mRxManager.register(d);
                    }

                    @Override
                    public void onNext(ICCIDList iccidList) {
                        if (iccidList != null) {
                            if (iccidList.getCode() == 200) {
                                if (lastNum.length() > 7) {
                                    mIView.setValiResult(lastNum);
                                } else {
                                    mIView.setICCIDList(iccidList);
                                }
                            } else {
                                mIView.showSystemToast(iccidList.getMsg());
                            }
                        } else {
                            mIView.setValiResultError(null);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        mIView.setValiResultError(null);
                    }

                    @Override
                    public void onComplete() {
                        mIView.loadDataComplated();
                    }
                });
    }

    @Override
    public void getQueryICCIDType() {
        RetrofitManager.getInstance().getQueryICCIDType(new HashMap<String, String>()).compose(RxHelper.<String>rxSchedulerHelper())
                .subscribe(new Observer<String>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        mRxManager.register(d);
                    }

                    @Override
                    public void onNext(String result) {
                        try {
                            JSONObject jsonObject = new JSONObject(result);
                            if ("200".equals(jsonObject.optString("code"))) {
                                JSONObject data = jsonObject.optJSONObject("data");
                                mIView.setICCIDTypeResult(data.optString("type"));
                            } else {
                                mIView.showSystemToast(jsonObject.optString("msg"));
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        mIView.loadDataError();
                    }

                    @Override
                    public void onComplete() {
                        mIView.loadDataComplated();
                    }
                });
    }

}
